/**
 * TSearchTaskControll.java V1.0 Copyright FUJITSU LIMITED 2018
 * 系统名          ：ProSign
 * 功能名          ：
 * 业务名          ：
 * ：
 * 履历            ：
 * NO 日期 Ver 更新者 内容
 * 1 2018年3月1日 V1.0 FNST 初版。
 */
package cn.aitrox.ry.common.config;


import com.github.abel533.mapperhelper.MapperInterceptor;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@ConditionalOnBean(DataSource.class)
public class MybatisConfiguration {

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(Environment environment) {

        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("cn.aitrox.ry.service.*.mapper");
        return mapperScannerConfigurer;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("cn.aitrox.ry.service.*.entity");
        // 分页插件
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("returnPageInfo", "check");
        properties.setProperty("params", "count=countSql");
        properties.setProperty("dialect", "mysql"); // 数据库方言
        properties.setProperty("rowBoundsWithCount", "true"); // 进行count查询
        pageHelper.setProperties(properties);

        // 通用mapper
        MapperInterceptor mapperInterceptor = new MapperInterceptor();
        Properties mapperProperties = new Properties();
        mapperProperties.setProperty("IDENTITY", "MYSQL"); // 主键回填
        mapperProperties.setProperty("mappers", "com.github.abel533.mapper.Mapper"); // mapper接口
        mapperInterceptor.setProperties(mapperProperties);

        // 设置插件
        Interceptor[] plugins = new Interceptor[]{pageHelper, mapperInterceptor};
        // 添加XML目录
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setPlugins(plugins);
        try {
            bean.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));
            SqlSessionFactory sqlSessionFactory = bean.getObject();
            // 驼峰自动映射
            sqlSessionFactory.getConfiguration().setMapUnderscoreToCamelCase(true);
            return sqlSessionFactory;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}